package com.jcfk.eam.controller.parent;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.*;

import com.jcfk.eam.domain.dto.base.EamAssetTypeDTO;
import com.jcfk.eam.domain.dto.main.EamAssetInfoDTO;
import com.jcfk.eam.domain.excel.main.EamAssetInfoExcel;
import com.jcfk.eam.domain.query.main.EamAssetInfoQuery;
import com.jcfk.eam.domain.uploadExcel.main.EamAssetInfoUploadExcel;
import com.jcfk.eam.service.base.EamAssetTypeService;
import com.jcfk.eam.service.main.EamAssetInfoService;
import com.jcfk.system.domain.query.SysLogQuery;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import com.alibaba.excel.EasyExcel;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;

import com.jcfk.common.core.utils.ExcelUtils;

import java.util.List;

/**
 * 资产台账
 *
 * @author libinghai
 * @email 35738025@gqq.com
 * @date 2024-07-28
 */
//@Api(tags = {"资产台账管理"})
@RestController
@RequestMapping("eamassetinfo")
public class EamAssetInfoController {
    @Autowired
    private EamAssetInfoService eamAssetInfoService;


    @Autowired
    private EamAssetTypeService eamAssetTypeService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamAssetInfoQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询资产台账")
    @PostMapping("/queryByPage")
    public RetPage<EamAssetInfoDTO> queryByPage(@RequestBody ReqPageParam<EamAssetInfoQuery> requestParam) {
        // 设置默认排序
        if (requestParam.getSort().getOrderItem() == null) {
            ReqSortParam<EamAssetInfoQuery> sortParam = new ReqSortParam();
            sortParam.setColumn("lastUpdateTime");
            sortParam.setOrderBy("desc");
            requestParam.setSort(sortParam);
        }

        // 设置设备类型查询条件
        if (!StrUtil.isEmpty(requestParam.getData().getAssetTypeId())) {
            EamAssetTypeDTO eamAssetTypeDTO = eamAssetTypeService.get(requestParam.getData().getAssetTypeId());
            if (eamAssetTypeDTO != null) {
                requestParam.getData().setAssetTypeId(eamAssetTypeDTO.getAllId());
            }
        }

        DataPage<EamAssetInfoDTO> page = new DataPage<EamAssetInfoDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
                EamAssetInfoDTO.class);
        page = eamAssetInfoService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     * 查询资产台账
     * @param requestParam
     * @return
     */
    public RetResult<List<EamAssetInfoDTO>> list(EamAssetInfoQuery requestParam) {
        return RetResult.ok(eamAssetInfoService.getList(requestParam));
    }
    /**
     * 资产台账
     * 根据id 查询详情
     */
    @ApiOperation("查询资产台账")
    @GetMapping("/get/{id}")
    public RetResult<EamAssetInfoDTO> get(@PathVariable("id") String id) {
        EamAssetInfoDTO item = eamAssetInfoService.getView(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理资产台账
     */
    @ApiOperation("保存资产台账")
    @PostMapping("/save")
    @RequestLogHeader(modular = "资产台账管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamAssetInfoDTO eamAssetInfo) {
        eamAssetInfoService.saveInfo(eamAssetInfo);
        return RetResult.ok();

    }

    /**
     * 批量删除资产台账
     */
    @ApiOperation("批量删除资产台账")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "资产台账管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamAssetInfoDTO eamAssetInfo) {
        return RetResult.ok(eamAssetInfoService.logicDeleteList(eamAssetInfo));
    }


    @PostMapping("export")
    @ApiOperation("导出")
    public void export(@RequestBody EamAssetInfoQuery requestParam, HttpServletResponse response) throws Exception {
        List<EamAssetInfoDTO> list = eamAssetInfoService.getList(requestParam);
        ExcelUtils.exportExcelToTarget(response, null, list, EamAssetInfoExcel.class);
    }

    public void export(@RequestBody EamAssetInfoQuery requestParam, HttpServletResponse response, Class myClass) throws Exception {
        List<EamAssetInfoDTO> list = eamAssetInfoService.queryByPage(requestParam);
        ExcelUtils.exportExcelToTarget(response, null, list, myClass);
    }

    /**
     * 导入资产台账
     */
    @PostMapping("upload")
    public RetResult<?> upload(MultipartFile file) throws IOException {
        // 读取excel
        List<EamAssetInfoUploadExcel> items = EasyExcel.read(file.getInputStream(), EamAssetInfoUploadExcel.class, null).autoCloseStream(true).sheet(0).headRowNumber(1).autoTrim(true).doReadSync();
        eamAssetInfoService.insertBatch(items);
        return RetResult.ok();
    }

    /**
     * 下载模板
     */
    @PostMapping("exportTemp")
    @ApiOperation("下载模板")
    public void exportTemp(HttpServletResponse response) throws Exception {
        List<EamAssetInfoUploadExcel> list = new ArrayList<>();
        list.add(new EamAssetInfoUploadExcel());
        ExcelUtils.exportExcelToTarget(response, null, list, EamAssetInfoUploadExcel.class);
    }


    /**
     * 删除资产台账
     */
    @ApiOperation("删除资产台账")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "资产台账管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamAssetInfoService.deleteitem(id);
        return RetResult.ok();
    }

}
